package com.lyf.scm.plan.domain.repository;

import cn.hutool.core.date.DateUtil;
import com.lyf.scm.common.constants.CommonConstants;
import com.lyf.scm.common.enums.ResponseMsg;
import com.lyf.scm.common.model.BaseSearchDTO;
import com.lyf.scm.common.model.SalesPlanSelectDTO;
import com.lyf.scm.plan.domain.remote.basedata.dto.AreaDTO;
import com.lyf.scm.plan.domain.remote.basedata.dto.AreaLevelDTO;
import com.lyf.scm.plan.domain.remote.basedata.facade.BaseDataFacade;
import com.lyf.scm.plan.domain.remote.bigdata.dto.LayeredSkuCodePageDTO;
import com.lyf.scm.plan.domain.remote.bigdata.dto.ProductListQueryDTO;
import com.lyf.scm.plan.domain.remote.bigdata.facade.BlOlapFacade;
import com.lyf.scm.plan.domain.remote.nateWork.facade.NateWorkFacade;
import com.lyf.scm.plan.domain.service.RedisCacheService;
import com.rome.arch.core.exception.RomeException;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * description:
 *
 * @author: haodong
 * @data: 2020/10/23 13:26
 */
@Service
public class BaseQueryRepository {

    @Resource
    private BaseDataFacade baseDataFacade;
    @Resource
    private NateWorkFacade nateWorkFacade;
    @Resource
    private BlOlapFacade blOlapFacade;
    @Resource
    private RedisCacheService redisCacheService;

    /**
     * 获取分层参数
     *
     * @param baseSearch
     */
    public void getLayeredData(BaseSearchDTO baseSearch) {
        //判断是否入参分层参数
        if (CollectionUtils.isNotEmpty(baseSearch.getLayeredCodeList())) {
            List<LayeredSkuCodePageDTO> layeredSkuList = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(baseSearch.getProvinceCodeList())) {
                List<AreaDTO> areaList = baseDataFacade.listAreaInfoByCode(baseSearch.getProvinceCodeList());
                areaList.forEach(pro -> {
                    ProductListQueryDTO productList = new ProductListQueryDTO();
                    productList.setProvince(pro.getName());
                    productList.setChannel(baseSearch.getChannelCode());
                    productList.setRankType(baseSearch.getRandType());
                    productList.setStartTime(DateUtil.formatDate(new Date()));
                    // productList.setStartTime("20201008");
                    productList.setPageNum(baseSearch.getPageNum());
                    productList.setPageSize(baseSearch.getPageSize());
                    productList.setLayeredType(baseSearch.getLayeredCodeList());
                    layeredSkuList.add(blOlapFacade.getProductList(productList));
                });
            }
            if (CollectionUtils.isNotEmpty(baseSearch.getCityCodeList())) {
                List<AreaDTO> areaList = baseDataFacade.listAreaInfoByCode(baseSearch.getCityCodeList());
                areaList.forEach(city -> {
                    ProductListQueryDTO productList = new ProductListQueryDTO();
                    productList.setCity(city.getName());
                    productList.setChannel(baseSearch.getChannelCode());
                    productList.setRankType(baseSearch.getRandType());
                    productList.setStartTime(DateUtil.formatDate(new Date()));
                    productList.setPageNum(baseSearch.getPageNum());
                    productList.setPageSize(baseSearch.getPageSize());
                    productList.setLayeredType(baseSearch.getLayeredCodeList());
                    layeredSkuList.add(blOlapFacade.getProductList(productList));
                });
            }
            if (CollectionUtils.isNotEmpty(baseSearch.getCountyCodeList())) {
                List<AreaLevelDTO> areaLevelList = baseDataFacade.listRelationByArea(baseSearch.getCountyCodeList());
                areaLevelList.forEach(con -> {
                    ProductListQueryDTO productList = new ProductListQueryDTO();
                    productList.setCity(con.getNameFullPath().split("-")[2]);
                    productList.setChannel(baseSearch.getChannelCode());
                    productList.setRankType(baseSearch.getRandType());
                    productList.setStartTime(DateUtil.formatDate(new Date()));
                    productList.setPageNum(baseSearch.getPageNum());
                    productList.setPageSize(baseSearch.getPageSize());
                    productList.setLayeredType(baseSearch.getLayeredCodeList());
                    layeredSkuList.add(blOlapFacade.getProductList(productList));
                });
            }
            if (CollectionUtils.isNotEmpty(baseSearch.getStoreCodeList())) {
                baseSearch.getStoreCodeList().forEach(store -> {
                    ProductListQueryDTO productList = new ProductListQueryDTO();
                    productList.setStore(store);
                    productList.setChannel(baseSearch.getChannelCode());
                    productList.setRankType(baseSearch.getRandType());
                    productList.setStartTime(DateUtil.formatDate(new Date()));
                    productList.setPageNum(baseSearch.getPageNum());
                    productList.setPageSize(baseSearch.getPageSize());
                    productList.setLayeredType(baseSearch.getLayeredCodeList());
                    layeredSkuList.add(blOlapFacade.getProductList(productList));
                });
            }

            List<String> skuCodeList = new ArrayList<>();
            layeredSkuList.forEach(item -> item.getDataList().forEach(list -> skuCodeList.add(list.getProductCode())));
            baseSearch.setSkuCodeList(skuCodeList);
        }

    }

    /**
     * 校验角色区域信息
     *
     * @param baseSearch
     * @return void
     **/
    public void checkPermissionArea(BaseSearchDTO baseSearch) {
        if (CollectionUtils.isEmpty(baseSearch.getPermissionAreaList())) {
            throw new RomeException(ResponseMsg.FAIL_2014.getCode(), ResponseMsg.FAIL_2014.getMsg());
        }

        //获取权限范围
        List<SalesPlanSelectDTO> permissionAreaList = baseSearch.getPermissionAreaList();
        List<String> pProvinceList = permissionAreaList.stream().filter(item -> item.getLevel().equals(CommonConstants.AREA_LEVEL_ONE)).map(SalesPlanSelectDTO::getCode).collect(Collectors.toList());
        List<String> pCityList = permissionAreaList.stream().filter(item -> item.getLevel().equals(CommonConstants.AREA_LEVEL_TWO)).map(SalesPlanSelectDTO::getCode).collect(Collectors.toList());
        List<String> pCountyList = permissionAreaList.stream().filter(item -> item.getLevel().equals(CommonConstants.AREA_LEVEL_THREE)).map(SalesPlanSelectDTO::getCode).collect(Collectors.toList());
        List<String> pStoreList = permissionAreaList.stream().filter(item -> item.getLevel().equals(CommonConstants.AREA_LEVEL_FOUR)).map(SalesPlanSelectDTO::getCode).collect(Collectors.toList());

        //如果有查询条件
        if (CollectionUtils.isNotEmpty(baseSearch.getAreaCodeList())) {
            //获取查询范围
            List<SalesPlanSelectDTO> areaCodeList = baseSearch.getAreaCodeList();
            List<String> qProvinceList = areaCodeList.stream().filter(item -> item.getLevel().equals(CommonConstants.AREA_LEVEL_ONE)).map(SalesPlanSelectDTO::getCode).collect(Collectors.toList());
            List<String> qCityList = areaCodeList.stream().filter(item -> item.getLevel().equals(CommonConstants.AREA_LEVEL_TWO)).map(SalesPlanSelectDTO::getCode).collect(Collectors.toList());
            List<String> qCountyList = areaCodeList.stream().filter(item -> item.getLevel().equals(CommonConstants.AREA_LEVEL_THREE)).map(SalesPlanSelectDTO::getCode).collect(Collectors.toList());
            List<String> qStoreList = areaCodeList.stream().filter(item -> item.getLevel().equals(CommonConstants.AREA_LEVEL_FOUR)).map(SalesPlanSelectDTO::getCode).collect(Collectors.toList());

            ifContainsAll(pProvinceList, qProvinceList);
            ifContainsAll(pCityList, qCityList);
            ifContainsAll(pCountyList, qCountyList);
            ifContainsAll(pStoreList, qStoreList);
            setAreaSearch(areaCodeList, baseSearch);
        } else {
            setAreaSearch(permissionAreaList, baseSearch);
        }
    }

    private void ifContainsAll(List<String> pCodeList, List<String> qCodeList) {
        if (CollectionUtils.isNotEmpty(pCodeList) && CollectionUtils.isNotEmpty(qCodeList)) {
            if (!pCodeList.containsAll(qCodeList)) {
                throw new RomeException(ResponseMsg.FAIL_1906.getCode(), ResponseMsg.FAIL_1906.getMsg());
            }
        }
        if (CollectionUtils.isEmpty(pCodeList) && CollectionUtils.isNotEmpty(qCodeList)) {
            if (!pCodeList.containsAll(qCodeList)) {
                throw new RomeException(ResponseMsg.FAIL_1906.getCode(), ResponseMsg.FAIL_1906.getMsg());
            }
        }
    }

    private void setAreaSearch(List<SalesPlanSelectDTO> areas, BaseSearchDTO baseSearchDTO) {
        int level = areas.get(0).getLevel();
        if (CommonConstants.AREA_LEVEL_ONE == level) {
            baseSearchDTO.setProvinceCodeList(areas.stream().map(SalesPlanSelectDTO::getCode).distinct().collect(Collectors.toList()));
        }
        if (CommonConstants.AREA_LEVEL_TWO == level) {
            baseSearchDTO.setCityCodeList(areas.stream().map(SalesPlanSelectDTO::getCode).distinct().collect(Collectors.toList()));
        }
        if (CommonConstants.AREA_LEVEL_THREE == level) {
            baseSearchDTO.setCountyCodeList(areas.stream().map(SalesPlanSelectDTO::getCode).distinct().collect(Collectors.toList()));
        }
        if (CommonConstants.AREA_LEVEL_FOUR == level) {
            baseSearchDTO.setStoreCodeList(areas.stream().map(SalesPlanSelectDTO::getCode).distinct().collect(Collectors.toList()));
        }
    }


}
